package com.ssl.admin.controller;

import com.ssl.admin.pojo.CommodityCategory;
import com.ssl.admin.service.CommodityCategoryService;
import com.ssl.common.response.CommonResponse;
import com.ssl.common.response.ResponseCode;
import com.ssl.common.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品种类控制器
 */
@RestController
@RequestMapping("/commodity/category")
public class CommodityCategoryController {
    @Autowired
    private CommodityCategoryService commodityCategoryService;

    /**
     * 1.查询商品类目
     */
    @GetMapping("/list")
    public CommonResponse getCategoryList(){
        try {
            return ResponseUtils.okResponse(commodityCategoryService.getCategoryList());
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null,
                    e.getMessage());
        }
    }

    /**
     * 2.新增商品类目
     * @param commodityCategory
     * @return
     */
    @PostMapping("/create")
    public CommonResponse createCategory(@RequestBody CommodityCategory commodityCategory) {
        try {
            commodityCategoryService.create(commodityCategory);
            return ResponseUtils.okCreatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    /**
     * 2.删除商品类目
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResponse deleteCategory(@PathVariable Long id) {
        try {
            commodityCategoryService.delete(id);
            return ResponseUtils.okDeletedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }
    /**
     * 更新商品类目
     */
    /**
     * 3.中间表新增
     */
    @PostMapping("/createBrandRelation")
    public CommonResponse createBrandRelation(@RequestParam(value = "brandId") Long brandId,
                                              @RequestParam(value = "categoryId") Long categoryId) {
        try {
            commodityCategoryService.createBrandRelation(brandId, categoryId);
            return ResponseUtils.okCreatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    /**
     * 4.中间表删除
     * @param brandId
     * @param categoryId
     * @return
     */
    @DeleteMapping("/deleteBrandRelation")
    public CommonResponse deleteBrandRelation(@RequestParam(value = "brandId") Long brandId,
                                              @RequestParam(value = "categoryId") Long categoryId) {
        try {
            commodityCategoryService.deleteBrandRelation(brandId, categoryId);
            return ResponseUtils.okDeletedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }
}
